package com.atguigu.lease.common.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.CompressionCodec;
import io.jsonwebtoken.CompressionCodecs;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;

import java.security.Key;
import java.util.Date;

public class JwtUtil {
    private static final Long EXPIRE_TTL_TIME =  60 * 60 * 1000L;
    private static final Key SECRET_KEY = Keys.hmacShaKeyFor("c28ae2498c3e625d0b3cf6fc55ddd44ae6334dbb".getBytes());
    public static String createToken(Long userId, String  userName) {
        return Jwts.builder()
                .setSubject("lease-userLogin")
                .setExpiration(new Date(System.currentTimeMillis() + EXPIRE_TTL_TIME * 1000))
                .claim("userId", userId)
                .claim("userName", userName)
                .signWith(SECRET_KEY)
                .compressWith(CompressionCodecs.GZIP) //压缩
                .compact();
    }
    public static Claims parseToken(String token) {
        return Jwts.parserBuilder()
                .setSigningKey(SECRET_KEY)
                .build()
                .parseClaimsJws(token)
                .getBody();
    }
}
